﻿using System.Globalization;
using System.Web;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using DotNetNuke.Services.Installer.Packages;
//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using DotNetNuke.ComponentModel;
using DotNetNuke.Entities.Tabs;

namespace DotNetNuke.Services.Localization
{

    public class LocaleController : ComponentBase<ILocaleController, LocaleController>, ILocaleController
    {

        #region "Private Shared Methods"

        private static object GetLocalesCallBack(CacheItemArgs cacheItemArgs)
        {
            int portalID = (int)cacheItemArgs.ParamList[0];
            Dictionary<string, Locale> locales = null;
            if (portalID > Null.NullInteger)
            {
                locales = CBO.FillDictionary<string, Locale>("CultureCode", DataProvider.Instance().GetLanguagesByPortal(portalID), new Dictionary<string, Locale>());
            }
            else
            {
                locales = CBO.FillDictionary<string, Locale>("CultureCode", DataProvider.Instance().GetLanguages(), new Dictionary<string, Locale>());
            }
            return locales;
        }

        #endregion

        #region "Public Methods"

        public bool CanDeleteLanguage(int languageId)
        {
            bool canDelete = true;
            foreach (PackageInfo package in PackageController.GetPackagesByType("CoreLanguagePack"))
            {
                LanguagePackInfo languagePack = LanguagePackController.GetLanguagePackByPackage(package.PackageID);
                if (languagePack.LanguageID == languageId)
                {
                    canDelete = false;
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
            return canDelete;
        }

        public List<CultureInfo> GetCultures(Dictionary<string, Locale> locales)
        {
            List<CultureInfo> cultures = new List<CultureInfo>();
            foreach (Locale locale in locales.Values)
            {
                cultures.Add(new CultureInfo(locale.Code));
            }
            return cultures;
        }

        public Locale GetCurrentLocale(int PortalId)
        {
            Locale locale = null;

            if (HttpContext.Current != null && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["language"]))
            {
                locale = GetLocale(HttpContext.Current.Request.QueryString["language"]);
            }
            if (locale == null)
            {
                if (PortalId == Null.NullInteger)
                {
                    locale = GetLocale(Localization.SystemLocale);
                }
                else
                {
                    locale = GetDefaultLocale(PortalId);
                }
            }

            return locale;
        }

        public Locale GetDefaultLocale(int portalId)
        {
            PortalInfo portal = new PortalController().GetPortal(portalId);
            Locale locale = null;
            if (portal != null)
            {
                Dictionary<string, Locale> locales = GetLocales(portal.PortalID);
                if (locales != null && locales.ContainsKey(portal.DefaultLanguage))
                {
                    locale = locales[portal.DefaultLanguage];
                }
            }
            if (locale == null)
            {
                //Load System default
                locale = GetLocale(Localization.SystemLocale);
            }
            return locale;
        }

        public Locale GetLocale(string code)
        {
            return GetLocale(Null.NullInteger, code);
        }

        public Locale GetLocale(int portalID, string code)
        {
            Dictionary<string, Locale> dicLocales = GetLocales(portalID);
            Locale locale = null;

            if (dicLocales != null)
            {
                dicLocales.TryGetValue(code, out locale);
            }

            return locale;
        }

        public Locale GetLocale(int languageID)
        {
            Dictionary<string, Locale> dicLocales = GetLocales(Null.NullInteger);
            Locale locale = null;

            foreach (KeyValuePair<string, Locale> kvp in dicLocales)
            {
                if (kvp.Value.LanguageId == languageID)
                {
                    locale = kvp.Value;
                    break; // TODO: might not be correct. Was : Exit For
                }
            }

            return locale;
        }

        public Dictionary<string, Locale> GetLocales(int portalID)
        {
            Dictionary<string, Locale> locales = new Dictionary<string, Locale>();

            if ((!(Globals.Status == Globals.UpgradeStatus.Install)))
            {
                string cacheKey = string.Format(DataCache.LocalesCacheKey, portalID.ToString());
                locales = CBO.GetCachedObject<Dictionary<string, Locale>>(new CacheItemArgs(cacheKey, DataCache.LocalesCacheTimeOut, DataCache.LocalesCachePriority, portalID), GetLocalesCallBack, true);
            }

            return locales;
        }

        public Dictionary<string, Locale> GetPublishedLocales(int portalID)
        {
            Dictionary<string, Locale> locales = new Dictionary<string, Locale>();

            foreach (KeyValuePair<string, Locale> kvp in GetLocales(portalID))
            {
                if (kvp.Value.IsPublished)
                {
                    locales.Add(kvp.Key, kvp.Value);
                }
            }

            return locales;
        }

        public bool IsEnabled(ref string localeCode, int portalId)
        {
            try
            {
                bool enabled = false;
                Dictionary<string, Locale> dicLocales = GetLocales(portalId);

                if ((!dicLocales.ContainsKey(localeCode)))
                {
                    //if localecode is neutral (en, es,...) try to find a locale that has the same language
                    if (localeCode.IndexOf("-") == -1)
                    {
                        foreach (string strLocale in dicLocales.Keys)
                        {
                            if (strLocale.Split('-')[0] == localeCode)
                            {
                                //set the requested _localecode to the full locale
                                localeCode = strLocale;
                                enabled = true;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    enabled = true;
                }
                return enabled;
            }
            catch (Exception ex)
            {
                //item could not be retrieved  or error
                Exceptions.Exceptions.LogException(ex);
                return false;
            }
        }

        public void UpdatePortalLocale(Locale locale)
        {
            DataProvider.Instance().UpdatePortalLanguage(locale.PortalId, locale.LanguageId, locale.IsPublished, UserController.GetCurrentUserInfo().UserID);
            DataCache.RemoveCache(string.Format(DataCache.LocalesCacheKey, locale.PortalId));
        }

        public bool IsDefaultLanguage(string code)
        {
            bool returnValue = false;
            if (code == PortalController.GetCurrentPortalSettings().DefaultLanguage)
            {
                returnValue = true;
            }
            return returnValue;
        }

        public void PublishLanguage(int portalid, string cultureCode, bool publish)
        {
            Dictionary<string, Locale> enabledLanguages = LocaleController.Instance.GetLocales(portalid);
            Locale enabledlanguage = null;
            if (enabledLanguages.TryGetValue(cultureCode, out enabledlanguage))
            {
                enabledlanguage.IsPublished = publish;
                LocaleController.Instance.UpdatePortalLocale(enabledlanguage);

                TabController tabCtrl = new TabController();

                tabCtrl.PublishTabs(TabController.GetTabsBySortOrder(portalid, cultureCode, false));
            }
        }

        #endregion

    }

}
